﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Transactions;

namespace DataExtract
{
    partial class Port 
    {
        public int OutMessage
        {
            get 
            {
                using (MasterDataContext db = MasterDataContext.GetDataContext()) 
                {
                    var msgSent = db.ExecuteQuery<MSG_Outbox>("select * from MSG_Outbox where CommPort="+PortName.ToString());
                    int sentNumber = 0;
                    foreach (MSG_Outbox record in msgSent) 
                    {
                        sentNumber += record.Receiver.Split(',').Length;
                    }

                    return sentNumber;
                }
            }
        }
    }

    partial class CaijiDataContext 
    {
        public static CaijiDataContext GetDataContext() 
        {
            string con = ConfigurationSettings.AppSettings["Con1"];
            return new CaijiDataContext(con);
        }
    }

    partial class MasterDataContext
    {
        public static MasterDataContext GetDataContext()
        {
            string con = ConfigurationSettings.AppSettings["Con2"];
            return new MasterDataContext(con);
        }
    }

    public class Tools 
    {
        public static void ChangeSetting(string key,string value) 
        {
            //读取程序集的配置文件
            string assemblyConfigFile = Assembly.GetEntryAssembly().Location;

            Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);
            //获取appSettings节点
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            //删除name，然后添加新值
            appSettings.Settings.Remove(key);
            appSettings.Settings.Add(key, value);

            //保存配置文件
            config.Save();

        }

        public static string ReadSetting(string key)
        {
            return ConfigurationManager.AppSettings[key];

        }
    }

}
